﻿using AutoMapper;
using ERPPublic;
using MediatR;
using Newtonsoft.Json;
using SqlSugar;
using WarehouseManagement.Application.Command.WarehouseSAppCommand;
using WarehouseManagement.Domain.DTO;
using WarehouseManagement.Domain.Entity;
using WarehouseManagement.Infrstructre.Interface;
using static ERPPublic.ParseAPI;

namespace WarehouseManagement.Application.CommandHandler.WarehouseSAppCommandHandler
{
    public class StrippingWarehousingStateCommandHander : IRequestHandler<StrippingWarehousingStateCommand, int>
    {
        lnterfaceWarehouseSAppRepository _warehouseSAppRepository;//仓库入库申请表
        lnterfaceStorageProductRepository _storageProductsRepository;//仓库入库申请产品明细表
        IMapper mapper;
        ISqlSugarClient db;

        public StrippingWarehousingStateCommandHander(lnterfaceWarehouseSAppRepository warehouseSAppRepository, lnterfaceStorageProductRepository storageProductsRepository, IMapper mapper, ISqlSugarClient db)
        {
            _warehouseSAppRepository = warehouseSAppRepository;
            _storageProductsRepository = storageProductsRepository;
            this.mapper = mapper;
            this.db = db;
        }

        public async Task<int> Handle(StrippingWarehousingStateCommand request, CancellationToken cancellationToken)
        {
            //开始事务
            db.Ado.BeginTran();
            try
            {
                //添加入库申请
                var ss = mapper.Map<WarehouseSApp>(request.dto);
                var a = await _warehouseSAppRepository.AddAsync(ss);
                foreach (var item in request.dto.list)
                {
                    StorageProduct storageProduct = new StorageProduct()
                    {
                        StorageNumber = ss.StorageNumber,
                        PNumber = item.PNumber,
                        PName = item.PName,
                        PSpec = item.PSpec,
                        PUnit = item.PUnit,
                        PPrice = item.PPrice,
                        SPConfirmNum = 66,
                        SumPrice = item.SumPrice,
                        Batch = item.Batch,
                        CreateDate = item.CreateDate,
                        ValidDate = item.ValidDate,
                        ArriveDate = item.ArriveDate,
                        WrapName = item.WrapName,
                        Remark = item.Remark
                    };
                    await _storageProductsRepository.AddAsync(storageProduct);
                };

                //退料表改状态
                List<Stripping> strippings = await GetStrippingList("", 0, null, null, 1, 1100);
                var stripp = strippings.Where(c => c.StripNumber == ss.StripNumber).FirstOrDefault();
                if (stripp != null)
                {
                    stripp.StripWState = 1;
                }
                var Updstrippings = await StrippingUpdate(stripp);
                ////废料表改状态
                //List<ProductionWaste> ProductionWastes = await GetProductionWasteList("", 0, null, 1, 1100);
                //var stripps = ProductionWastes.Where(c => c.ProductionWasteNumber == ss.StripNumber).FirstOrDefault();
                //if (stripps != null)
                //{
                //    stripps.PIsStorage = 1; 
                //}
                //var Updstripping = await ProductionWasteUpdate(stripps);


                MessageCreateDto dto = new MessageCreateDto();
                dto.MType = "确认入库";
                dto.Title = "确认入库新增";
                dto.SendTime = DateTime.Now;
                await MessageCreates(dto);

                
                //提交事务
                db.Ado.CommitTran();
                return 1;
            }
            catch (Exception)
            {
                //回滚事务
                db.Ado.RollbackTran();
                throw;
            }
        }

        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="dtos"></param>
        /// <returns></returns>
        public async Task<int> MessageCreates(MessageCreateDto dtos)
        {
            string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI1MCIsInNpZCI6IjE4NjcwOTU3MzQ0NjIwNTg0OTYiLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL2dyb3Vwc2lkIjoiMTksMTkiLCJqdGkiOiI1MGYyMWRmZS0yNWQ3LTQ1OTQtOTg1Yi0wZTM2MDhhZjJiNmYiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoi5LuY5rSb5ZiJIiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvdXJpIjoiLyIsImlzcyI6IlJlbmFvYm8yMDA0IiwiYXVkIjoiV2VpZnV3dTIyMDNBIn0._wh23n1M7asMGST9NyeZj-5rk5ICAiOFv6yDk6e2o7Y";
            HttpClientWrapper cilent = new HttpClientWrapper(token);
            string chanpin = "http://116.205.121.197:8000/erpcp/api/Message/SendMessage";
            string jsonstr = JsonConvert.SerializeObject(dtos);
            string postReponse = await cilent.PostAsync(chanpin, jsonstr);
            return 1;
        }


        /// <summary>
        /// 显示退料信息
        /// </summary>
        /// <param name="StripNumber"></param>
        /// <param name="StripStart"></param>
        /// <param name="StripEnd"></param>
        /// <param name="StripWState"></param>
        /// <param name="pindex"></param>
        /// <param name="psize"></param>
        /// <returns></returns>
        public async Task<dynamic> GetStrippingList(string? StripNumber,int? StripWState, DateTime? StripStart, DateTime? StripEnd, int pindex, int psize)
        {
            string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI1MCIsInNpZCI6IjE4NjcwOTU3MzQ0NjIwNTg0OTYiLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL2dyb3Vwc2lkIjoiMTksMTkiLCJqdGkiOiI1MGYyMWRmZS0yNWQ3LTQ1OTQtOTg1Yi0wZTM2MDhhZjJiNmYiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoi5LuY5rSb5ZiJIiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvdXJpIjoiLyIsImlzcyI6IlJlbmFvYm8yMDA0IiwiYXVkIjoiV2VpZnV3dTIyMDNBIn0._wh23n1M7asMGST9NyeZj-5rk5ICAiOFv6yDk6e2o7Y";
            HttpClientWrapper cilent = new HttpClientWrapper(token);

            string chanpin = "http://116.205.121.197:8000/sc/api/Stripping/GetStrippProduct?StripNumber=" + StripNumber + "&StripWState=" + StripWState + "&StripStart=" + StripStart + "&StripEnd=" + StripEnd + "&Pindex=" + pindex + "&Psize=" + psize;

            string postReponse = await cilent.GetAsync(chanpin);
            var json = JsonConvert.DeserializeObject<ApiResultList<Stripping>>(postReponse);
            return json.data.list;
        }
        ///// <summary>
        ///// 显示废料信息
        ///// </summary>
        ///// <returns></returns>
        //public async Task<dynamic> GetProductionWasteList(string? ProductionWasteNumber, int? StripWState, DateTime? ProductionDate, int pindex, int psize)
        //{
        //    string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI1MCIsInNpZCI6IjE4NjcwOTU3MzQ0NjIwNTg0OTYiLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL2dyb3Vwc2lkIjoiMTksMTkiLCJqdGkiOiI1MGYyMWRmZS0yNWQ3LTQ1OTQtOTg1Yi0wZTM2MDhhZjJiNmYiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoi5LuY5rSb5ZiJIiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvdXJpIjoiLyIsImlzcyI6IlJlbmFvYm8yMDA0IiwiYXVkIjoiV2VpZnV3dTIyMDNBIn0._wh23n1M7asMGST9NyeZj-5rk5ICAiOFv6yDk6e2o7Y";
        //    HttpClientWrapper cilent = new HttpClientWrapper(token);

        //    string chanpin = "http://116.205.121.197:8000/sc/api/Stripping/GetStrippProduct?StripNumber="+ ProductionWasteNumber+ "&StripWState=" + StripWState+ "&ProductionDate=" + ProductionDate+ "&Pindex=" + pindex + "&Psize=" + psize;

        //    string postReponse = await cilent.GetAsync(chanpin);
        //    var json = JsonConvert.DeserializeObject<ApiResultList<Stripping>>(postReponse);
        //    return json.data.list;
        //}
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="Strippingobj"></param>
        /// <returns></returns>
        public async Task<dynamic> StrippingUpdate(Stripping Strippingobj)
        {
            string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI1MCIsInNpZCI6IjE4NjcwOTU3MzQ0NjIwNTg0OTYiLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL2dyb3Vwc2lkIjoiMTksMTkiLCJqdGkiOiI1MGYyMWRmZS0yNWQ3LTQ1OTQtOTg1Yi0wZTM2MDhhZjJiNmYiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoi5LuY5rSb5ZiJIiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvdXJpIjoiLyIsImlzcyI6IlJlbmFvYm8yMDA0IiwiYXVkIjoiV2VpZnV3dTIyMDNBIn0._wh23n1M7asMGST9NyeZj-5rk5ICAiOFv6yDk6e2o7Y";
            HttpClientWrapper cilent = new HttpClientWrapper(token);

            string chanpin = "http://116.205.121.197:8000/sc/api/Stripping/UpdateStrippProduct";
            string json = JsonConvert.SerializeObject(Strippingobj);
            string postReponse = await cilent.PostAsync(chanpin, json);
            return postReponse;
        }


        //退料
        public class Stripping:Audit
        {
            /// <summary>
            /// 退料Id
            /// </summary>
            public int StrippingId { get; set; }
            /// <summary>
            /// 生产退料编号
            /// </summary>
            [SugarColumn(ColumnDataType = "Nvarchar(50)")]//设计长度
            public string? StripNumber { get; set; }
            /// <summary>
            /// 入库状态
            /// </summary>
            public int StripWState { get; set; }
            /// <summary>
            /// 单据编号
            /// </summary>
            [SugarColumn(ColumnDataType = "Nvarchar(50)")]//设计长度
            public string? StripDocumentNumber { get; set; }
            /// <summary>
            /// 退料日期
            /// </summary>
            public DateTime? StripDate { get; set; }
            /// <summary>
            /// 退料数量合计
            /// </summary>
            public int StripNumTotal { get; set; }




        }
        ///// <summary>
        ///// 废料修改
        ///// </summary>
        ///// <param name="Strippingobj"></param>
        ///// <returns></returns>
        //public async Task<dynamic> ProductionWasteUpdate(ProductionWaste Strippingobj)
        //{
        //    string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI1MCIsInNpZCI6IjE4NjcwOTU3MzQ0NjIwNTg0OTYiLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL2dyb3Vwc2lkIjoiMTksMTkiLCJqdGkiOiI1MGYyMWRmZS0yNWQ3LTQ1OTQtOTg1Yi0wZTM2MDhhZjJiNmYiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoi5LuY5rSb5ZiJIiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvdXJpIjoiLyIsImlzcyI6IlJlbmFvYm8yMDA0IiwiYXVkIjoiV2VpZnV3dTIyMDNBIn0._wh23n1M7asMGST9NyeZj-5rk5ICAiOFv6yDk6e2o7Y";
        //    HttpClientWrapper cilent = new HttpClientWrapper(token);

        //    string chanpin = "http://116.205.121.197:8000/sc/api/Scraps/UpdScrapsProduct";
        //    string json = JsonConvert.SerializeObject(Strippingobj);
        //    string postReponse = await cilent.PostAsync(chanpin, json);
        //    return postReponse;
        //}
        ///// <summary>
        ///// 生产废料表
        ///// </summary>
        //public class ProductionWaste : Audit
        //{
        //    /// <summary>
        //    /// 生产废料id
        //    /// </summary>
        //    [SugarColumn(IsIdentity = true, IsPrimaryKey = true)]//自增主键
        //    public int PWId { get; set; }
        //    /// <summary>
        //    /// 单据编号
        //    /// </summary>
        //    [SugarColumn(ColumnDataType = "Nvarchar(50)")]//设计长度
        //    public string? PDocumentNumber { get; set; }

        //    /// <summary>
        //    /// 生产废料编号
        //    /// </summary>
        //    public string? ProductionWasteNumber { get; set; }

        //    /// <summary>
        //    /// 生产废料主题
        //    /// </summary>
        //    public string? ProductionWasteTopic { get; set; }
        //    /// <summary>
        //    /// 废料日期
        //    /// </summary>
        //    public DateTime? ProductionDate { get; set; }
        //    /// <summary>
        //    /// 是否入库
        //    /// </summary>
        //    public int PIsStorage { get; set; }
        //    /// <summary>
        //    /// 制单人员
        //    /// </summary>
        //    [SugarColumn(ColumnDataType = "Nvarchar(50)")]//设计长度
        //    public string? PDocumentMakers { get; set; }
        //    /// <summary>
        //    /// 备注
        //    /// </summary>
        //    [SugarColumn(ColumnDataType = "Nvarchar(200)")]//设计长度
        //    public string? PRemark { get; set; }

        //}
    }
}
